<?php
	/* Functions in this file */
	/**************************/

	// ClearDaCapo()
	// ClearDalSegno()
	// ClearDalSegnoSegno()
	// ClearNumber($number)
	// DoDeserialize(&$stream)
	// DoSerialize(&$stream)
	// GetMFCClassName()
	// GetMFCClassSchema()
	// GetNumbers()
	// GetNumberText($number)
	// GetText()
	// IsDaCapoSet()
	// IsDalSegnoSegnoSet()
	// IsDalSegnoSet()
	// IsNumberSet($number)
	// SetDaCapo()
	// SetDalSegno()
	// SetDalSegnoSegno()
	// SetNumber($number)
	// SetNumbers($numbers)
?>
<?php
	
	 
	 
	class AlternateEnding extends SystemSymbol {
		 
		const daCapo = 9;
		const dalSegno = 10;
		const dalSegnoSegno = 11;
		const numbersMask = 0x000007ff;
		 
		function GetMFCClassName() {
			return "CSectionSymbol";
		}
		 
		function GetMFCClassSchema() {
			return 1;
		}
		 
		 
		function AlternateEnding ($system = NULL, $position = NULL, $numbers = NULL) {
			 
			$this->SetSystem($system);
			$this->SetPosition($position);
			$this->SetNumbers($numbers);
			 
		}
		 
		public static function IsValidNumbers ($numbers) {
			return (($numbers == 0) || ((($numbers & self::numbersMask) != 0) && (($numbers & ~self::numbersMask) == 0)));
		}
		 
		public static function IsValidNumber ($number) {
			return (($number >= 1) && ($number <= self::dalSegnoSegno));
		}
		 
		 
		function SetNumbers($numbers) {
			
			if (!$this->IsValidNumbers($numbers)) return 0;
			$this->m_data = MAKELONG(0, $numbers);
			return 1;
		}
		 
		 
		 
		function GetNumbers() {
			 
			return (HIWORD($this->m_data));
		}
		 
		 
		 
		 
		function SetNumber($number) {
			 
			if (!$this->IsValidNumber($number)) return 0;
			 
			// Note: Numbers are stored in zero-based form
			$numbers = $this->GetNumbers();
			$numbers |= (pow(2, ($number - 1)));
			 
			return ($this->SetNumbers($numbers));
		}
		 
		 
		 
		 
		function IsNumberSet($number) {
			 
			//------Last Checked------//
			// - Jan 12, 2005
			if (!$this->IsValidNumber($number)) return 0;
			 
			// Number is one based, so subtract one
			$number--;
			 
			// Determine if bit is set
			$numbers = $this->GetNumbers();
			$power = pow(2, $number);
			return (($numbers & $power) == $power);
		}
		 
		 
		 
		 
		function ClearNumber($number) {
			 
			//------Last Checked------//
			// - Jan 12, 2005
			if (!$this->IsValidNumber($number)) return 0;
			 
			$numbers = $this->GetNumbers();
			$numbers &= ~(pow(2, ($number - 1)));
			$this->SetNumbers($numbers);
			return 1;
		}
		 
		// Da Capo Functions
		function SetDaCapo() {
			$this->SetNumber(self::daCapo);
		}
		function IsDaCapoSet() {
			return ($this->IsNumberSet(self::daCapo));
		}
		function ClearDaCapo() {
			$this->ClearNumber(self::daCapo);
		}
		 
		// Dal Segno Functions
		function SetDalSegno() {
			$this->SetNumber(self::dalSegno);
		}
		function IsDalSegnoSet() {
			return ($this->IsNumberSet(self::dalSegno));
		}
		function ClearDalSegno() {
			$this->ClearNumber(self::dalSegno);
		}
		 
		// Dal Segno Segno Functions
		function SetDalSegnoSegno() {
			$this->SetNumber(self::dalSegnoSegno);
		}
		function IsDalSegnoSegnoSet() {
			return ($this->IsNumberSet(self::dalSegnoSegno));
		}
		function ClearDalSegnoSegno() {
			$this->ClearNumber(self::dalSegnoSegno);
		}
		
		/*
			function DoSerialize(PowerTabOutputStream& stream){
			
		    //------Last Checked------//
		    // - Dec 27, 2004
		    return (SystemSymbol::$this->DoSerialize(stream));
		}
		
		
		
		public function DoDeserialize(&$stream){
			
		    	return parent::DoDeserialize($stream);
		}*/
		
		// Operations
		function GetText() {
			 
			$returnValue = "";
			 
			$groupStart = -1;
			$groupEnd = -1;
			 
			// Construct the numbers
			$i = 1;
			$lastNumber = 8;
			for (; $i <= $lastNumber; $i++) {
				 
				$numberSet = $this->IsNumberSet($i);
				 
				if ($numberSet) {
					// Starting a new group of numbers
					if ($groupStart == -1)
						$groupStart = $groupEnd = $i;
					// Continuing existing group
					else
						$groupEnd = $i;
				}
				 
				// Always treat the last number like the end of a group
				if ($i == $lastNumber)
					$numberSet = FALSE;
				 
				// We've reached the end of a group, if groupStart != -1, then we have a group
				if (!$numberSet && $groupStart != -1) {
					// Add a separator
					if ($returnValue)
						$returnValue .= ", ";
					 
					$temp = "";
					 
					// Single number
					if ($groupStart == $groupEnd)
						$temp = sprintf("%s.", $this->GetNumberText($groupStart));
					// 2 numbers
					elseif ($groupStart == ($groupEnd - 1))
					$temp = sprintf("%s., %s.", $this->GetNumberText($groupStart), $this->GetNumberText($groupEnd));
					// > 2 numbers
					else
						$temp = sprintf("%s.-%s.", $this->GetNumberText($groupStart), $this->GetNumberText($groupEnd));
					 
					$returnValue .= $temp;
					 
					// Reset the group data
					$groupStart = $groupEnd = -1;
				}
			}
			 
			// Construct the special symbols
			$i = self::daCapo;
			for (; $i <= self::dalSegnoSegno; $i++) {
				if ($this->IsNumberSet($i)) {
					if ($returnValue)
						$returnValue .= ", ";
					$returnValue .= $this->GetNumberText($i);
				}
				 
			}
			 
			return $returnValue;
		}
		 
		 
		protected function GetNumberText($number) {
			 
			assert($this->IsValidNumber($number));
			 
			if ($number == self::daCapo)
				return "D.C.";
			elseif ($number == self::dalSegno)
			return "D.S.";
			elseif ($number == self::dalSegnoSegno)
			return "D.S.S.";
			return sprintf("%d", $number);
			 
		}
		 
	}
	 
	 
?>


